package com.example.yue.appclent.util;

import com.example.yue.appclent.client.bean.ClientMessBean;
import com.example.yue.appclent.client.bean.ServerMessBean;

import org.json.JSONException;
import org.json.JSONObject;


public class JsonUtil {

	public static String getJsonString(ServerMessBean serverMessBean) {

		JSONObject jsonObject = new JSONObject();

		try {
			jsonObject.put("time", serverMessBean.getTime());
			jsonObject.put("name", serverMessBean.getName());
			jsonObject.put("host", serverMessBean.getHost());
			jsonObject.put("mess", serverMessBean.getMess());
			jsonObject.put("messFlag", serverMessBean.getMessFlag());
			jsonObject.put("cmdMess", serverMessBean.getCmdMess());
			jsonObject.put("otherMess", serverMessBean.getOtherMess());
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return jsonObject.toString();
	}

	public static String getJsonString(ClientMessBean clientMessBean){
		JSONObject jsonObject = new JSONObject();

		try {
			jsonObject.put("time", clientMessBean.getTime());
			jsonObject.put("name", clientMessBean.getName());
			jsonObject.put("host", clientMessBean.getHost());
			jsonObject.put("mess", clientMessBean.getMess());
			jsonObject.put("messFlag", clientMessBean.getMessFlag());
			jsonObject.put("otherMess", clientMessBean.getOtherMess());
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return jsonObject.toString();
	}

	public static ServerMessBean getServerMessBean(String str)  {

		try {
			JSONObject jsonObject = new JSONObject(str);
			long time = 0;
			String name = null;
			String host = null;
			String mess = null;
			int messFlag = 0;
			String cmdMess = null;
			String otherMess = null;

			if (jsonObject.has("time")) {
                time = jsonObject.getLong("time");
            }
			if (jsonObject.has("name")) {
                name = jsonObject.getString("name");
            }
			if (jsonObject.has("host")) {
                host = jsonObject.getString("host");
            }
			if (jsonObject.has("mess")) {
                mess = jsonObject.getString("mess");
            }
			if (jsonObject.has("messFlag")) {
                messFlag = jsonObject.getInt("messFlag");
            }
			if (jsonObject.has("cmdMess")) {
                cmdMess = jsonObject.getString("cmdMess");
            }
			if (jsonObject.has("otherMess")) {
                otherMess = jsonObject.getString("otherMess");
            }
            return new ServerMessBean(time, name, host, mess, messFlag, cmdMess, otherMess);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static ClientMessBean getClientMessBean(String str)  {

		try {
			long time = 0;
			String name = null;
			String host = null;
			String mess = null;
			int messFlag = 0;
			String otherMess = null;

			JSONObject jsonObject = new JSONObject(str);

			if (jsonObject.has("time")) {
                time = jsonObject.getLong("time");
            }
			if (jsonObject.has("name")) {
                name = jsonObject.getString("name");
            }
			if (jsonObject.has("host")) {
                host = jsonObject.getString("host");
            }
			if (jsonObject.has("mess")) {
                mess = jsonObject.getString("mess");
            }
			if (jsonObject.has("messFlag")) {
                messFlag = jsonObject.getInt("messFlag");
            }

			if (jsonObject.has("otherMess")) {
                otherMess = jsonObject.getString("otherMess");
            }
            return new ClientMessBean(time, name, host, mess, messFlag, otherMess);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}
}
